package server.business.service.Impl;

import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.business.mapper.PatientBackInformationMapper;
import server.business.pojo.PatientBackInformation;
import server.business.service.PatientBackInformationService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 针对表【patient_back_information(患者退血信息表)】的数据库操作Service实现
 *
 * @Author 赵越
 * @Time 2022-08-01
 */
@Service("patientWithdrawalInformationService")
public class PatientBackInformationServiceImpl extends ServiceImpl<PatientBackInformationMapper, PatientBackInformation> implements PatientBackInformationService {

    /**
     * 引入患者退血信息数据库操作
     */
    @Autowired
    private PatientBackInformationMapper patientBackInformationMapper;

    /**
     * 查询所有患者退血信息
     *
     * @return 返回患者退血信息列表
     */
    @Override
    public List<PatientBackInformation> findAll() {
        return patientBackInformationMapper.findAll();
    }

    /**
     * 保存患者退血信息
     *
     * @param patientBackInformation 患者退血信息包装类
     */
    @Override
    public void savePatientBackInformation(PatientBackInformation patientBackInformation) {
        patientBackInformationMapper.insert(patientBackInformation);
    }

    /**
     * 根据退血单号得到患者退血信息
     *
     * @param number 退血单号
     * @return 返回患者退血信息
     */
    @Override
    public PatientBackInformation getBackInformationByNumber(String number) {
        return patientBackInformationMapper.getBackInformationByNumber(number);
    }

    /**
     * 根据退血单号设置退血信息
     *
     * @param number                 退血单号
     * @param patientBackInformation 退血信息
     */
    @Override
    public void setInformationByNumber(String number, PatientBackInformation patientBackInformation) {
        patientBackInformationMapper.setInformationByNumber(number, patientBackInformation);
    }

    /**
     * 根据退血单号设置审核人、审核人工号、审核时间
     *
     * @param number        退血单号
     * @param auditor       审核人
     * @param auditorNumber 审核人工号
     * @param auditTime     审核时间
     */
    @Override
    public void setAuditByNumber(String number, String auditor, String auditorNumber, Date auditTime) {
        patientBackInformationMapper.setAuditByNumber(number, auditor, auditorNumber, auditTime);
    }

    /**
     * 分页查询功能
     *
     * @param currentPage            当前页面
     * @param pageSize               页面显示个数
     * @param patientBackInformation 患者退血信息
     * @return 返回分页查询结果
     */
    @Override
    public IPage<PatientBackInformation> getPage(int currentPage, int pageSize, PatientBackInformation patientBackInformation) {
        LambdaQueryWrapper<PatientBackInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(patientBackInformation.getBackInformationNumber()), PatientBackInformation::getBackInformationNumber, patientBackInformation.getBackInformationNumber())
                .eq(Strings.isNotBlank(patientBackInformation.getPatientId()), PatientBackInformation::getPatientId, patientBackInformation.getPatientId())
                .eq(Strings.isNotBlank(patientBackInformation.getTransfusionNumber()), PatientBackInformation::getTransfusionNumber, patientBackInformation.getTransfusionNumber())
                .eq(Strings.isNotBlank(patientBackInformation.getBackBloodReason()), PatientBackInformation::getBackBloodReason, patientBackInformation.getBackBloodReason())
                .eq(Strings.isNotBlank(patientBackInformation.getApplicantWorkNumber()), PatientBackInformation::getApplicantWorkNumber, patientBackInformation.getApplicantWorkNumber())
                .like(patientBackInformation.getApplyTime() != null, PatientBackInformation::getApplyTime, patientBackInformation.getApplyTime())
                .eq(Strings.isNotBlank(patientBackInformation.getAuditorNumber()), PatientBackInformation::getAuditorNumber, patientBackInformation.getAuditorNumber())
                .like(patientBackInformation.getAuditTime() != null, PatientBackInformation::getAuditTime, patientBackInformation.getAuditTime());
        Page<PatientBackInformation> page = new Page<>(currentPage, pageSize);
        patientBackInformationMapper.selectPage(page, queryWrapper);
        return page;
    }

    /**
     * 分页查询功能
     *
     * @param currentPage            当前页面
     * @param pageSize               页面显示个数
     * @param patientBackInformationList 患者退血信息
     * @return 返回分页查询结果
     */
    @Override
    public IPage<PatientBackInformation> getPageIn(int currentPage, int pageSize, List<PatientBackInformation> patientBackInformationList) {
        LambdaQueryWrapper<PatientBackInformation> queryWrapper = new LambdaQueryWrapper<>();
        ArrayList<Long> ids = new ArrayList<>();
        for (PatientBackInformation patientBackInformation : patientBackInformationList) {
            try {
                ids.add(patientBackInformation.getId());
            } catch (Exception e) {
                System.out.println("没有您想查询的数据！");
            }
        }
        queryWrapper.in(ids.size() != 0, PatientBackInformation::getId, ids);
        Page<PatientBackInformation> page = new Page<>(currentPage, pageSize);
        return page;
    }

    /**
     * 根据编号批量下载选中数据
     *
     * @param response 请求
     * @param ids      编号列表
     */
    @Override
    public void batchDownload(HttpServletResponse response, ArrayList<Long> ids) throws IOException {
        List<PatientBackInformation> patientBackInformations = patientBackInformationMapper.selectBatchIds(ids);
        // 设置文本内容
        try {
            response.setContentType("application/vnd.ms-excel");
            String fileName = URLEncoder.encode("患者退血申请审批信息" + System.currentTimeMillis(), "UTF-8");
            // 设置字符编码
            response.setCharacterEncoding("utf-8");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcelFactory.write(response.getOutputStream(),
                    PatientBackInformation.class).sheet("患者退血申请审批信息")
                    .doWrite(patientBackInformations);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据编号批量下载选中数据
     *
     * @param response 请求
     * @param ids      编号列表
     */
    @Override
    public void batchDownloadIn(HttpServletResponse response, ArrayList<Long> ids) throws IOException {
        List<PatientBackInformation> patientBackInformations = patientBackInformationMapper.selectBatchIds(ids);
        // 设置文本内容
        try {
            response.setContentType("application/vnd.ms-excel");
            String fileName = URLEncoder.encode("患者退血入库信息" + System.currentTimeMillis(), "UTF-8");
            // 设置字符编码
            response.setCharacterEncoding("utf-8");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcelFactory.write(response.getOutputStream(),
                    PatientBackInformation.class).sheet("患者退血入库信息")
                    .doWrite(patientBackInformations);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}




